package db

import (
	"d-serv-go/server/core/bean"
	"d-serv-go/server/core/consts"
	"d-serv-go/server/core/util"
)

const (
	/**_ADD_:保存是否为添加模式 '#_ADD_#'=='true'*/
	ADD_FLAG = "_ADD_"
	/**_AUTOADD_:byid查找不存在时自动添加一条记录*/
	AUTO_ADD_FLAG = "_AUTOADD_"
	/**_PAGE_: 分页包装标签 */
	QUERY_PAGE = "_PAGE_"
	/**_HIDE_: 列表隐藏字段 */
	QUERY_HIDE = "_HIDE_"
	/**_DEL_:是否强制真删除 */
	DELETE_DROP_FLAG = "_DEL_"
	/**_NOPAGE_:查询设定不分页 */
	QUERY_NOPAGE_FLAG = "_NOPAGE_"
	/**_OLDBEAN_:保存对应的原始数据信息 */
	SAVE_OLD_DATA = "_OLDBEAN_"
	/**_TRANS_:是否启用新事务 */
	TRANS_FLAG = "_TRANS_"
	/**_CLIENT_:是否来自手机等客户端app */
	APP_FLAG = "_APP_"
)

type ParamBean struct {
	bean.Bean
}

func NewParamBean(def ...*bean.Bean) *ParamBean {
	if len(def) > 0 {
		return &ParamBean{Bean: *def[0]}
	}
	return &ParamBean{Bean: *bean.NewBean("")}
}

func NewParamBean2(serv, act string) *ParamBean {
	b := &ParamBean{
		Bean: *bean.NewBean(""),
	}
	b.SetServId(serv)
	b.Set(consts.PARAM_ACT_CODE, act)
	return b
}

func (p *ParamBean) SetServId(servId string) *ParamBean {
	p.Set(consts.PARAM_SERV_ID, servId)
	return p
}

func (p *ParamBean) GetServId() string {
	return p.GetStr(consts.PARAM_SERV_ID)
}

func (p *ParamBean) GetAct() string {
	return p.GetStr(consts.PARAM_ACT_CODE)
}

func (p *ParamBean) SetAct(act string) *ParamBean {
	p.Set(consts.PARAM_ACT_CODE, act)
	return p
}

func (p *ParamBean) GetSelect() string {
	return p.GetStr(consts.PARAM_SELECT, "")
}

func (p *ParamBean) SetSelect(s string) *ParamBean {
	p.Set(consts.PARAM_SELECT, s)
	return p
}

func (p *ParamBean) SetTable(table string) *ParamBean {
	p.Set(consts.PARAM_SELECT, table)
	return p
}

func (p *ParamBean) GetTable() string {
	return p.GetStr(consts.PARAM_TABLE)
}

func (p *ParamBean) SetTransFlag(b bool) *ParamBean {
	p.Set(consts.TRANS_FLAG, b)
	return p
}

func (p *ParamBean) GetTransFlag() bool {
	return p.GetBool(consts.TRANS_FLAG)
}

func (p *ParamBean) GetQueryPage() *bean.PageBean {
	var page *bean.PageBean
	if p.Contains(QUERY_PAGE) {
		if pg, ok := p.Get(QUERY_PAGE).(*bean.PageBean); ok {
			return pg
		}
	} else { //初始化页面配置，设定缺省值
		page = bean.NewPageBean()
		p.Set(QUERY_PAGE, page)
	}
	return page
}

func (p *ParamBean) GetShowNum() int {
	return p.GetInt(consts.PARAM_ROWNUM)
}

func (p *ParamBean) GetNowPage() int {
	return p.GetInt(consts.PAGE_NOWPAGE, 1)
}

func (p *ParamBean) SetShowNum(count int) *ParamBean {
	p.Set(consts.PARAM_ROWNUM, count)
	return p
}

func (p *ParamBean) SetNowPage(page int) *ParamBean {
	p.Set(consts.PAGE_NOWPAGE, page)
	return p
}

func (p *ParamBean) GetQueryNoPageFlag() bool {
	return p.GetBool(QUERY_NOPAGE_FLAG, false)
}

func (p *ParamBean) GetQueryHide() string {
	return p.GetStr(QUERY_HIDE, "")
}

func (p *ParamBean) GetQueryPageOrder() string {
	return p.GetQueryPage().GetOrder()
}

func (p *ParamBean) GetOrder() string {
	return p.GetStr(consts.PARAM_ORDER)
}

func (p *ParamBean) GetGroupBy() string {
	return p.GetStr("_groupBy", "")
}

//func (p *ParamBean) GetFibertCtx() *fiber.Ctx {
//	return p.Get(consts.FIBER_CONTEXT).(*fiber.Ctx)
//}
//
//func (p *ParamBean) GetContext() *Context {
//	ctx := p.Get(consts.CONTEXT)
//	if ctx != nil {
//		return ctx.(*Context)
//	}
//	return nil
//}

func (p *ParamBean) SetOrder(order string) *ParamBean {
	p.Set(consts.PARAM_ORDER, order)
	return p
}

func (p *ParamBean) SetAddFlag(b bool) *ParamBean {
	p.Set(ADD_FLAG, b)
	return p
}

func (p *ParamBean) GetAddFlag() bool {
	return p.GetBool(ADD_FLAG)
}

func (p *ParamBean) SetBatchSaveDatas(dataList []*bean.Bean) *ParamBean {
	p.Set("BATCHDATAS", dataList)
	return p
}

func (p *ParamBean) SetLinkFlag(linkFlag bool) *ParamBean {
	p.Set(consts.PARAM_LINK_FLAG, linkFlag)
	return p
}

func (p *ParamBean) GetSaveFullData() *bean.Bean {
	var fullBean *bean.Bean
	if len(p.GetId()) > 0 { // 添加模式，增加原有数据值
		oldBean := p.GetSaveOldData()
		fullBean = util.BeanUtil.MergeBean(oldBean, &p.Bean)
	}
	return fullBean
}

func (p *ParamBean) GetSaveOldData() *bean.Bean {
	return p.GetBean(SAVE_OLD_DATA)
}

func (p *ParamBean) SetBatchSaveDelIds(delIds string) *ParamBean {
	p.Set("BATCHDELS", delIds)
	return p
}
